package com.idealighter.game.server.db.datadb.dao;

import com.google.inject.Inject;
import com.google.inject.Singleton;
import com.idealighter.game.core.constant.group.GroupActiveConstant;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupDomain;
import com.idealighter.game.server.db.datadb.generate.domain.RoomCardGroupDomainExample;
import com.idealighter.game.server.db.datadb.mapper.RoomCardGroupMapper;
import com.idealighter.utils.time.TimeUtil;
import java.util.List;

@Singleton
public class RoomCardGroupDao {
  private RoomCardGroupMapper mapper;

  @Inject
  public RoomCardGroupDao(RoomCardGroupMapper mapper) {
    this.mapper = mapper;
  }


  /**
   * 获取群列表 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:17:13
   * @param ids id列表.
   * @return 结果.
   */
  public List<RoomCardGroupDomain> selectByIdList(List<Integer> ids) {
    RoomCardGroupDomainExample example = new RoomCardGroupDomainExample();
    RoomCardGroupDomainExample.Criteria criteria = example.createCriteria();

    example.setOrderByClause("id desc");

    criteria.andIdIn(ids);

    return mapper.selectByExample(example);
  }

  /**
   * @Title updateGroupRoomCardNum.
   * @Description 更新房卡数量
   * @date 2018年6月21日 上午11:45:29
   * @param groupId 群编号
   * @param num 房间数量
   * @return int 更新数量
   */
  public int updateGroupRoomCardNum(int groupId, int num) {
    RoomCardGroupDomain record = new RoomCardGroupDomain();
    record.setId(groupId);
    record.setRoomNum(num);
    return mapper.updateByPrimaryKeySelective(record);
  }

  /**
   * @Title incGroupRoomNum.
   * @Description 增加房间数量
   * @date 2018年6月21日 上午11:51:07
   * @param groupId 群编号
   * @param incNum 增加的数量
   * @return int 更新的数量
   */
  public int incGroupRoomNum(int groupId, int incNum) {
    return mapper.increaseRoomNum(groupId, incNum);
  }

  /**
   * @Title decGroupRoomNum.
   * @Description 减少房间数量
   * @author houdongsheng
   * @date 2018年6月21日 上午11:51:47
   * @param groupId 群编号
   * @param decNum 减少的数量
   * @return 更新的记录条数
   */
  public int decGroupRoomNum(int groupId, int decNum) {
    return mapper.decreaseRoomNum(groupId, decNum);
  }



  /**
   * 关闭 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:17:33
   * @param groupId 群id.
   * @param ownerId 群主id.
   * @return 影响条数.
   */
  public int close(int groupId, long ownerId) {
    RoomCardGroupDomainExample example = new RoomCardGroupDomainExample();
    RoomCardGroupDomainExample.Criteria criteria = example.createCriteria();

    criteria.andIdEqualTo(groupId).andOwnerIdEqualTo(ownerId);

    RoomCardGroupDomain update = new RoomCardGroupDomain();
    update.setEndTime(TimeUtil.now());
    update.setState(GroupActiveConstant.CLOSE);

    return mapper.updateByExampleSelective(update, example);
  }


  public RoomCardGroupDomain selectByPrimaryKey(Integer id) {
    return mapper.selectByPrimaryKey(id);
  }

  public int insertSelective(RoomCardGroupDomain domain) {
    return mapper.insertSelective(domain);
  }

  public int increaseMembers(int groupId) {
    return mapper.increaseMembers(groupId);
  }

  public int decreaseMembers(int groupId) {
    return mapper.decreaseMembers(groupId);
  }

  /**
   * 更新名字 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:18:03
   * @param groupId 群id.
   * @param ownerId 群主id.
   * @param groupName 群名称.
   * @return 影响条数.
   */
  public int updateName(int groupId, long ownerId, String groupName) {
    RoomCardGroupDomainExample example = new RoomCardGroupDomainExample();
    RoomCardGroupDomainExample.Criteria criteria = example.createCriteria();

    criteria.andIdEqualTo(groupId).andOwnerIdEqualTo(ownerId);

    RoomCardGroupDomain update = new RoomCardGroupDomain();
    update.setGroupName(groupName);

    return mapper.updateByExampleSelective(update, example);
  }


  public int deleteById(Integer groupId) {

    return mapper.deleteByPrimaryKey(groupId);
  }


  /**
   * 转让群 .
   *
   * @author abin
   * @date 2018年5月23日 下午4:18:32
   * @param groupId 群id.
   * @param ownerId 群主id.
   * @param toPlayerId 目标id.
   * @param toPlayerSuperId 目标靓号.
   * @param toPlayerName 目标名.
   * @return 影响条数.
   */
  public int updateOwner(Integer groupId, Long ownerId, Long toPlayerId, Long toPlayerSuperId,
      String toPlayerName) {
    RoomCardGroupDomainExample example = new RoomCardGroupDomainExample();
    RoomCardGroupDomainExample.Criteria criteria = example.createCriteria();

    criteria.andIdEqualTo(groupId).andOwnerIdEqualTo(ownerId);

    RoomCardGroupDomain update = new RoomCardGroupDomain();
    update.setOwnerId(toPlayerId);
    update.setOwnerSuperId(toPlayerSuperId);
    update.setOwnerPlayerName(toPlayerName);

    return mapper.updateByExampleSelective(update, example);
  }
}
